package com.lin.eat.serivce.impl;

import com.lin.eat.entry.*;
import com.lin.eat.dao.CustomerMapper;
import com.lin.eat.serivce.CustomerService;
import com.lin.eat.util.FileUtil;
import com.lin.eat.util.JwtUtil;
import com.lin.eat.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Service
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 顾客注册
     * @param c_id
     * @param c_name
     * @param c_phone
     * @param c_password
     * @return
     */
    @Override
    public ServerResponse<Customer> customerRegister(String c_id, String c_name, String c_phone, String c_password) {

        ServerResponse<Customer> serverResponse = null;
        Customer customer = customerMapper.findCustomerByPhone(c_phone);

        if(customer != null){
            serverResponse = ServerResponse.createByError(null);
        }else {
            int i = customerMapper.addCustomer(c_id, c_name, c_phone,c_password, ImagePath.DEFAULT_PATH);
            serverResponse = i > 0? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        }
        return serverResponse;
    }

    /**
     * 顾客登录
     * @param c_phone
     * @param c_password
     * @return
     */
    @Override
    public ServerResponse<Customer> customerLogin(String c_phone, String c_password){

        ServerResponse<Customer> serverResponse = null;
        Customer customer = customerMapper.findCustomerByPP(c_phone, c_password);

        if(customer != null){
            String token = JwtUtil.createToken(customer.getC_id(), customer.getC_phone());
            customerMapper.updateTokenByCid(customer.getC_id(),token);
            customer.setC_token(token);
            customer.setAddressList(null);
            customer.setC_password(null);
            serverResponse = ServerResponse.createBySuccess(customer);
        }else {
            serverResponse = ServerResponse.createByError(null);
        }
        return serverResponse;
    }

    /**
     * 顾客退出登录
     * @param c_token
     * @return
     */
    @Override
    public ServerResponse<Customer> customerLogout(String c_token) {
        ServerResponse<Customer> serverResponse = null;
        int i = customerMapper.updateTokenByToken(c_token);
        serverResponse = i > 0? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 获取顾客信息
     * @param c_token
     * @return
     */
    @Override
    public ServerResponse<Customer> getCustomerInfo(String c_token){
        Customer customer = customerMapper.findCustomerByToken(c_token);
        ServerResponse<Customer> serverResponse = null;
        if (customer == null) {
            serverResponse = ServerResponse.createByError(null);
        }else {
            List<Address> addressList = customerMapper.findAddressByCid(customer.getC_id());
            customer.setAddressList(addressList);
            customer.setC_password(null);
            customer.setC_token(null);
            serverResponse = ServerResponse.createBySuccess(customer);
        }
        return serverResponse;
    }

    /**
     * 顾客上传头像
     * @param file
     * @param token
     * @return
     * @throws IOException
     */
    @Override
    public ServerResponse<Customer> updateHeadImage(MultipartFile file, String token) throws IOException {
        ServerResponse<Customer> serverResponse = null;
        String imageUrl = FileUtil.uploadHead(file, token);
        String Cid = JwtUtil.parseJwt(token).getId();
        int i = customerMapper.updateHeadImageByCid(Cid,imageUrl);
        Customer customer = customerMapper.findCustomerByCid(Cid);
        customer.setAddressList(null);
        customer.setC_password(null);
        customer.setC_token(null);
        serverResponse = i > 0? ServerResponse.createBySuccess(customer) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 更新用户名和性别
     * @param c_name
     * @param c_sex
     * @param token
     * @return
     */
    @Override
    public ServerResponse<Customer> updateCustomerInfo(String c_name, int c_sex, String token) {
        ServerResponse<Customer> serverResponse = null;
        String Cid = JwtUtil.parseJwt(token).getId();
        int i = customerMapper.updateCustomerInfo(c_name, c_sex, Cid);
        Customer customer = customerMapper.findCustomerByCid(Cid);
        customer.setAddressList(null);
        customer.setC_password(null);
        customer.setC_token(null);
        serverResponse = i > 0? ServerResponse.createBySuccess(customer) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 新增收货地址
     * @param address
     * @param token
     * @return
     */
    @Override
    public ServerResponse addAddress(Address address, String token) {
        ServerResponse serverResponse = null;
        String Cid = JwtUtil.parseJwt(token).getId();
        address.setAd_id(UuidUtil.createUUID());
        address.setC_id(Cid);
        //不允许重复地址
        Address a = customerMapper.findAddressByInfo(address.getAd_info());
        if(a != null){
            serverResponse = ServerResponse.createByError(null);
        } else {
            int i = customerMapper.addAddress(address);
            int result = 0;
            for(String s : address.getAd_num()){
                int num = customerMapper.addAddressNum(UuidUtil.createUUID(), address.getAd_id(), s);
                if (num > 0)
                    result++;
            }
            serverResponse = i>0&&result==3? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        }
        return serverResponse;
    }

    /**
     * 获取全部收货地址
     * @param token
     * @return
     */
    @Override
    public ServerResponse<List<Address>> getAllAddress(String token) {
        ServerResponse<List<Address>> serverResponse = null;
        try {
            String Cid = JwtUtil.parseJwt(token).getId();
            List<Address> addressList = customerMapper.selectAllAddressByCid(Cid);
            for (Address address : addressList){
                List<String> num = customerMapper.selectAllAddressNumByAdid(address.getAd_id());
                address.setAd_num(num);
            }
            serverResponse = ServerResponse.createBySuccess(addressList);
        } catch (Exception e){
            serverResponse = ServerResponse.createByError(null);
        } finally {
            return serverResponse;
        }
    }

    /**
     * 修改收货地址信息
     * @param address
     * @return
     */
    @Override
    public ServerResponse updateAddressInfo(Address address) {
        ServerResponse serverResponse = null;
        int i = customerMapper.updateAddressInfo(address);
        int k = customerMapper.deleteAddressNumByAdid(address.getAd_id());
        int result = 0;
        for(String s : address.getAd_num()){
            int num = customerMapper.addAddressNum(UuidUtil.createUUID(), address.getAd_id(), s);
            if (num > 0)
                result++;
        }
        serverResponse = i>0&&k==3&&result==3? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 设置为默认地址
     * @param ad_id
     * @return
     */
    @Override
    public ServerResponse updateAddressState(String ad_id) {
        ServerResponse serverResponse = null;
        int result = customerMapper.setOtherStateZero();
        int i = customerMapper.updateAddressState(ad_id);
        serverResponse = i>0&&result>=0? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

    /**
     * 删除地址
     * @param ad_id
     * @return
     */
    @Override
    public ServerResponse deleteAddress(String ad_id) {
        ServerResponse serverResponse = null;
        int i = customerMapper.deleteAddress(ad_id);
        serverResponse = i>0? ServerResponse.createBySuccess(null) : ServerResponse.createByError(null);
        return serverResponse;
    }

}
